package com.clqteam.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.clqteam.dao.VIPDao;
import com.clqteam.model.VIP;
import com.clqteam.util.JDBCutil;

public class VIPDaoImpl implements VIPDao{

	@Override
	public VIP getVIPInfoByVId(int vId) {
		VIP vip = new VIP();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT s_vip.vId,s_vip.vPoint,s_vip.pitRate,");
		sql.append(" s_vip.vDiscount,s_vip.vTime FROM s_vip");
		sql.append(" WHERE vId = ?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, vId);
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				vip.setvId(rs.getInt("vId"));
				vip.setPitRate(rs.getDouble("pitRate"));
				vip.setvDiscount(rs.getDouble("vDiscount"));
				vip.setvPoint(rs.getInt("vPoint"));
				vip.setvTme(rs.getString("vTime"));
			}
			JDBCutil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		return vip;
	}

	@Override
	public int getCountAllVIP() {
		int total = 0;
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT COUNT(vId) AS total FROM s_vip");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				total = rs.getInt("total");
			}
			JDBCutil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return total;
	}

	@Override
	public List<VIP> getAllVIPInfo() {
		List<VIP> vips = new ArrayList<VIP>();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT s_vip.vId,s_vip.vPoint,s_vip.pitRate,");
		sql.append(" s_vip.vDiscount,s_vip.vTime FROM s_vip");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				VIP vip = new VIP();
				vip.setvId(rs.getInt("vId"));
				vip.setPitRate(rs.getDouble("pitRate"));
				vip.setvDiscount(rs.getDouble("vDiscount"));
				vip.setvPoint(rs.getInt("vPoint"));
				vip.setvTme(rs.getString("vTime"));
				vips.add(vip);
			}
			JDBCutil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return vips;
	}

}
